package servlet.admin;

import com.google.gson.Gson;
import entity.Admins;
import service.AdminsService;
import service.Impl.AdminsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/admin/AdminFingByName")
public class AdminFingByNameServlet extends HttpServlet {
    private AdminsService adminsService = new AdminsServiceImpl();
    private Gson gson = new Gson(); // 创建 Gson 实例

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String realName = req.getParameter("realName");
        List<Admins> admins = adminsService.findByName(realName);

        // 设置响应内容类型为 JSON
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");

        // 将管理员列表转换为 JSON 字符串
        String json = gson.toJson(admins);

        // 写入响应
        resp.getWriter().write(json);
    }
}
